Pass statbufs down to xdg_mime_get_mime_type_for_file() where possible, to
authorMatthias Clasen <mclasen@redhat.com>
Thu, 1 Sep 2005 14:42:02 +0000 (14:42 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Thu, 1 Sep 2005 14:42:02 +0000 (14:42 +0000)
2005-09-01  Matthias Clasen  <mclasen@redhat.com>

* gtk/gtkfilesystemunix.c: Pass statbufs down to
xdg_mime_get_mime_type_for_file() where possible, to avoid
useless re-stating.

ChangeLog
ChangeLog.pre-2-10
gtk/gtkfilesystemunix.c
gtk/xdgmime/ChangeLog
gtk/xdgmime/test-mime.c
gtk/xdgmime/xdgmime.c
gtk/xdgmime/xdgmime.h

index 6aeb2414cd437b1873a9ba07dd24cc6d8e8abf09..129fbb0cb6d2c47ff5ddc31c15263ac56960afef 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2005-09-01  Matthias Clasen  <mclasen@redhat.com>
 
+       * gtk/gtkfilesystemunix.c: Pass statbufs down to 
+       xdg_mime_get_mime_type_for_file() where possible, to avoid
+       useless re-stating.  
+       
        * gtk/gtkaction.c (gtk_action_get_accel_closure): Fix doc
        typo.  (#314921, Guillaume Cottenceau)
 
index 6aeb2414cd437b1873a9ba07dd24cc6d8e8abf09..129fbb0cb6d2c47ff5ddc31c15263ac56960afef 100644 (file)
@@ -1,5 +1,9 @@
 2005-09-01  Matthias Clasen  <mclasen@redhat.com>
 
+       * gtk/gtkfilesystemunix.c: Pass statbufs down to 
+       xdg_mime_get_mime_type_for_file() where possible, to avoid
+       useless re-stating.  
+       
        * gtk/gtkaction.c (gtk_action_get_accel_closure): Fix doc
        typo.  (#314921, Guillaume Cottenceau)
 
index 89c22d470d8cd634a8254297589f46a8ea166808..9525efcb73c5cc6682638640941eaa96fc0c2ed5 100644 (file)
@@ -1224,7 +1224,7 @@ get_icon_type_from_path (GtkFileSystemUnix *system_unix,
 
   icon_type = get_icon_type (filename, NULL);
   if (icon_type == ICON_REGULAR)
-    *mime_type = xdg_mime_get_mime_type_for_file (filename);
+    *mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
 
   return icon_type;
 }
@@ -1923,7 +1923,7 @@ create_stat_info_entry_and_emit_add (GtkFileFolderUnix *folder_unix,
     entry->statbuf = *statbuf;
 
   if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0)
-    entry->mime_type = g_strdup (xdg_mime_get_mime_type_for_file (filename));
+    entry->mime_type = g_strdup (xdg_mime_get_mime_type_for_file (filename, statbuf));
 
   g_hash_table_insert (folder_unix->stat_info,
                       g_strdup (basename),
@@ -2002,7 +2002,7 @@ gtk_file_folder_unix_get_info (GtkFileFolder      *folder,
        }
 
       if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
-       mime_type = xdg_mime_get_mime_type_for_file (filename);
+       mime_type = xdg_mime_get_mime_type_for_file (filename, &statbuf);
       else
        mime_type = NULL;
 
@@ -2150,9 +2150,12 @@ cb_fill_in_mime_type (gpointer key, gpointer value, gpointer user_data)
   struct stat_info_entry *entry = value;
   GtkFileFolderUnix *folder_unix = user_data;
   char *fullname = g_build_filename (folder_unix->filename, basename, NULL);
+  struct stat *statbuf = NULL;
 
-  /* FIXME: Should not need to re-stat.  */
-  const char *mime_type = xdg_mime_get_mime_type_for_file (fullname);
+  if (folder_unix->have_stat)
+    statbuf = &entry->statbuf;
+
+  const char *mime_type = xdg_mime_get_mime_type_for_file (fullname, statbuf);
   entry->mime_type = g_strdup (mime_type);
 
   g_free (fullname);
index efaa26cc3f93011596e2db226f0bbe1d585dcfb1..0c8a7a79cf2f188b37282995ea79c6546e62324d 100644 (file)
@@ -1,3 +1,11 @@
+2005-09-01  Matthias Clasen  <mclasen@redhat.com>
+
+       * xdgmime.h:
+       * xdgmime.c (xdg_mime_get_mime_type_for_file): Take
+       a struct statbuf * as argument.
+
+       * test-mime.c (main): Adjust.
+
 2005-08-24  Matthias Clasen  <mclasen@redhat.com>
 
        * === Released 2.8.2 ===
index b0bcc875fe43159260e6d119276414fbe2639d59..9364cf82383399666858a7692b6fd0da698b0e5b 100644 (file)
@@ -108,7 +108,7 @@ main (int argc, char *argv[])
   for (i = 1; i < argc; i++)
     {
       file_name = argv[i];
-      result = xdg_mime_get_mime_type_for_file (file_name);
+      result = xdg_mime_get_mime_type_for_file (file_name, NULL);
       printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
     }
 
index c7ac59290ab63f654e91cf0093d51ccc4e541e22..bc8eeaf01ade8718fd5287ed5fd6a7203695168f 100644 (file)
@@ -447,14 +447,15 @@ xdg_mime_get_mime_type_for_data (const void *data,
 }
 
 const char *
-xdg_mime_get_mime_type_for_file (const char *file_name)
+xdg_mime_get_mime_type_for_file (const char  *file_name,
+                                 struct stat *statbuf)
 {
   const char *mime_type;
   FILE *file;
   unsigned char *data;
   int max_extent;
   int bytes_read;
-  struct stat statbuf;
+  struct stat buf;
   const char *base_name;
 
   if (file_name == NULL)
@@ -473,10 +474,17 @@ xdg_mime_get_mime_type_for_file (const char *file_name)
   if (mime_type != XDG_MIME_TYPE_UNKNOWN)
     return mime_type;
 
-  if (stat (file_name, &statbuf) != 0)
-    return XDG_MIME_TYPE_UNKNOWN;
+  if (!statbuf)
+    {
+      if (stat (file_name, &buf) != 0)
+       return XDG_MIME_TYPE_UNKNOWN;
+
+      statbuf = &buf;
+    }
+  else
+    printf ("don't restat\n");
 
-  if (!S_ISREG (statbuf.st_mode))
+  if (!S_ISREG (statbuf->st_mode))
     return XDG_MIME_TYPE_UNKNOWN;
 
   /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
index 92ee60e26a14f995e0ed143e312ad751a6082dd4..d07cfded9d13fe1ba4d884933ebed67a518496d8 100644 (file)
@@ -30,6 +30,7 @@
 #define __XDG_MIME_H__
 
 #include <stdlib.h>
+#include <sys/stat.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -69,7 +70,8 @@ extern const char *xdg_mime_type_unknown;
 
 const char  *xdg_mime_get_mime_type_for_data       (const void *data,
                                                    size_t      len);
-const char  *xdg_mime_get_mime_type_for_file       (const char *file_name);
+const char  *xdg_mime_get_mime_type_for_file       (const char *file_name,
+                                                    struct stat *statbuf);
 const char  *xdg_mime_get_mime_type_from_file_name (const char *file_name);
 int          xdg_mime_is_valid_mime_type           (const char *mime_type);
 int          xdg_mime_mime_type_equal              (const char *mime_a,